package coms.example.demo.controller;

import coms.example.demo.domain.User;
import coms.example.demo.serive.UserServie;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
@RequestMapping("/demo")
public class UserController {
          @ApiOperation(value="创建用户", notes="根据User对象创建用户")
          @ApiImplicitParams({
                  @ApiImplicitParam(dataType = "java.lang.Long", name = "id", value = "id", required = true, paramType = "path"),
                  @ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", required = true)
          })
          @RequestMapping(value = "/user/{id}",method = RequestMethod.POST)
          public User insert(@PathVariable Long id, @RequestBody User user){

               System.out.println("id:"+id+", user:"+user);
               user.setId(id);

               return user;
          }

          @ApiOperation(value="获取指定id用户详细信息", notes="根据user的id来获取用户详细信息")
          @ApiImplicitParam(name = "id",value = "用户id", dataType = "String", paramType = "path")
          @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
          public User getUser(@PathVariable Long id){

               User user = new User();
               user.setId(id);
               user.setPassword("abc");
               user.setUsername("12345");
               return user;
          }

          @ApiOperation(value="获取所有用户详细信息", notes="获取用户列表信息")
          @RequestMapping(value = "/users", method = RequestMethod.GET)
          public List<User> getUserList(){

               List<User> list = new ArrayList<>();
               User user = new User();
               user.setId(15L);
               user.setPassword("ricky");
               user.setUsername("root");

               list.add(user);

               return list;
          }

          @ApiIgnore
          @ApiOperation(value="删除指定id用户", notes="根据id来删除用户信息")
          @ApiImplicitParam(name = "id",value = "用户id", dataType = "java.lang.Long", paramType = "path")
          @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
          public String delete(@PathVariable Long id){
               System.out.println("delete user:"+id);
               return "OK";
          }


          @Autowired
     private UserServie userServie;


     @RequestMapping("/")
     @ResponseBody
     public String hello(){
          return "hello";
     }

     @RequestMapping("/getList")
     public String getList(Model model){
          List<User> userList = userServie.getUserList();
          model.addAttribute("userList",userList);
          return "showlist";
     }
     @RequestMapping("/addUser")
     public String addUser(User user){
          System.out.println(user);
          userServie.add(user);
          return "redirect:/getList";
     }
     @RequestMapping("/toAdd")
     public String toSave(){
          return "add";
     }
     @RequestMapping("/toEdit")
     public String toedit(Integer id,Model model){
          User user = userServie.getUserByid(id);
          model.addAttribute("user",user);
          return "edit";
     }
     @RequestMapping("/delete")
     public String toDelete(Integer id){
          userServie.delete(id);
          return "redirect:/getList";
     }

     @RequestMapping("/update")
     public  String update(User user){
          userServie.edit(user);
          return "redirect:/getList";
     }


}
